package com.briup.jd2407cms.web.controller;

import com.briup.jd2407cms.result.Result;
import com.briup.jd2407cms.result.ResultCode;
import com.briup.jd2407cms.utils.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传
 */
@Api(tags = "文件上传")
@RestController
@RequestMapping("/auth")
@Slf4j
public class FileUploadController {
    @Autowired
    UploadUtil uploadUtil;

    @ApiOperation("上传图片")
    @PostMapping("/upload")
    public Result<?> upload(@RequestPart MultipartFile img){
        try {
            String url = uploadUtil.fileToOSS(img);
            return Result.success(url);
        }catch (Exception e){
            log.error("图片文件上传失败！");
            return Result.failure(ResultCode.FAIL);
        }
    }
}
